package com.flyee.viewer
{
	import com.flyee.controller.UIController;
	import com.flyee.events.PanelStateChangeEvent;
	import com.flyee.model.Charactor;
	import com.flyee.service.CharactorService;
	import com.flyee.service.SceneService;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.GenerateIDTool;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Dictionary;
	
	import mx.containers.HBox;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.RadioButton;
	import mx.controls.TextInput;
	import mx.core.Application;
	import mx.core.UIComponent;
	
	public class LoginViewer extends BaseWindowViewer implements Destroy
	{
		public static var NAME:String = "com.flyee.viewer.LoginViewer";
		public var vid : String = GenerateIDTool.generateID();
		
		private var selectedImage:Image = null; 
		private var imageDiction:Dictionary = null;
		private var radioDiction:Dictionary = null;
		private var radioBox:HBox = null;
		private var imageBox:HBox = null;
		private var tipLabel:Label = null;
		private var nameInput:TextInput = null;
		private var sureButton:Button = null;
		public function LoginViewer()
		{
			super();
			this.layout = "vertical";
			this.setStyle("horizontalAlign","center");
			this.setStyle("verticalAlign","middle");
			this.setStyle("verticalGap",10);
			this.width = 400;
			this.height = 300;
			this.x = (Application.application.width - this.width)/2;
			this.y = (Application.application.height - this.height)/2;
			init();
		}
		
		private function init():void{
			tipLabel = new Label();
			nameInput = new TextInput();
			
			tipLabel.text = "请输入你的昵称";
			imageDiction = new Dictionary();
			radioDiction = new Dictionary();
			imageDiction["manOne"] = createImage("manOne","resource/man1.png");
			imageDiction["manTwo"] = createImage("manTwo","resource/man2.png");
			imageDiction["womanOne"] = createImage("womanOne","resource/woman1.png");
			imageDiction["womanTwo"] = createImage("womanTwo","resource/woman2.png");
			radioDiction["manOne"] = createRadio("manOne");
			radioDiction["manTwo"] = createRadio("manTwo");
			radioDiction["womanOne"] = createRadio("womanOne");
			radioDiction["womanTwo"] = createRadio("womanTwo");
			radioBox = new HBox();
			radioBox.percentWidth = 100;
			radioBox.setStyle("horizontalAlign","center");
			imageBox = new HBox();
			imageBox.percentWidth = 100;
			imageBox.setStyle("horizontalAlign","center");
			
			for(var tag:String in imageDiction){
				imageBox.addChild(imageDiction[tag]);
			}
			
			for(var temp:String in radioDiction){
				radioBox.addChild(radioDiction[temp]);
			}
			
			sureButton = new Button();
			sureButton.label = "确定";
			sureButton.addEventListener(MouseEvent.CLICK,loginEvent);
			
			this.addChild(tipLabel);
			this.addChild(nameInput);
			this.addChild(imageBox);
			this.addChild(radioBox);
			this.addChild(sureButton);
			this.addEventListener(PanelStateChangeEvent.REMOVE,removeEvent);
			
			selectImage("manOne");
		}
		
		private function createImage(name:String,source:String):Image{
			var image:Image = new Image();
			image.percentWidth = 25;
			image.autoLoad = true;
			image.scaleContent = true;
			image.source = source;
			image.name = name;
			image.setStyle("horizontalAlign","center");
			image.addEventListener(MouseEvent.CLICK,appearanceSelectEvent);
			return image;
		}
		
		private function createRadio(name:String):RadioButton{
			var radio:RadioButton = new RadioButton();
			radio.groupName = "appear";
			radio.name = name;
			radio.percentWidth = 25;
			radio.setStyle("textAlign","center");
			radio.addEventListener(MouseEvent.CLICK,appearSelectEvent);
			return radio;
		}
		
		private function getGlowFilter():GlowFilter{
			var color:int = 0xff3300;
			var alpha:Number = 0.8;
			var blurx:int = 5;
			var blury:int = 5;
			var strength:int = 3;
			var glowFilter:GlowFilter = new GlowFilter(color,alpha,blurx,blury,strength);
			return glowFilter;
		}
		
		private function appearSelectEvent(event:Event):void{
			var target:RadioButton = event.currentTarget as RadioButton;
			selectImage(target.name);	
		}
		
		private function selectImage(target:String):void{
			if(selectedImage){
				selectedImage.filters = null;
				selectedImage = null;
			}
			selectedImage = imageDiction[target];
			var temp:RadioButton = radioDiction[target];
			temp.selected = true;
			selectedImage.filters = [getGlowFilter()];
		}
		
		private function appearanceSelectEvent(event:Event):void{
			var target:Image = event.currentTarget as Image;
			selectImage(target.name);	
		}
		
		private function loginEvent(event:MouseEvent):void{
        	var charactorService:CharactorService = ObjectCollection.beansContext.getObject("charactorService") as CharactorService;
        	var sceneService:SceneService = ObjectCollection.beansContext.getObject("sceneService") as SceneService;
        	var nickName:String = nameInput.text;
            ObjectCollection.master = new Charactor();
            ObjectCollection.master.id = "g" + Math.round(Math.random() * 100);
            ObjectCollection.master.gcode = ObjectCollection.master.id;
            if(nickName){
            	 ObjectCollection.master.name = nickName;
            }else{
            	 ObjectCollection.master.name = "游客" + ObjectCollection.master.id;
            }
            ObjectCollection.master.mySex = 1;
            ObjectCollection.master.x = 0;
            ObjectCollection.master.y = 0;
            ObjectCollection.master.sceneId = ObjectCollection.DEFAULT_SCENE_ID;
			ObjectCollection.master.sceneInstanceId = ObjectCollection.DEFAULT_SCENE_ID;

            charactorService.login(ObjectCollection.master);
            var uiController:UIController = ObjectCollection.beansContext.getObject("uiController") as UIController;
        	uiController.rootOwner = ObjectCollection.uiContainer as UIComponent;
        	uiController.removedFromDesktopByName(LoginViewer.NAME);
        	uiController.addToDesktopByName(MainMenuViewer.NAME);
         }
         
         private function removeEvent(event:Event):void{
         	dispose();
         }
		
		public override function dispose():void
		{
			super.dispose();
		}
		
	}
}